package com.young.financial.service.impl;

import com.young.financial.config.UrlConstants;
import com.young.financial.constants.FinancialPayConfigEnumConstants;
import com.young.financial.service.YsWechatPayService;
import com.young.financial.utils.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

/**
 * @api
 * @apiExample author zhaopengjun
 * tar
 * date  2019/11/2015:44
 * @apiGroup
 * @apiVersion
 * @apiHeader
 * @apiParam
 * @apiSuccess
 */
public class YsWechatPayServiceImpl implements YsWechatPayService {
    @Value("${balancePlatform.platfromMarking}")
    private String platfromMarking;
    @Autowired
    private HttpClientUtils httpClientUtils;
    @Autowired
    private AccessTokenUtil accessTokenUtil;
    @Autowired
    private RedisUtils redisUtils;
    public static final Logger logger = LoggerFactory.getLogger(YsWechatPayServiceImpl.class);

    @Override
    public Map<String, Object> yswechatpay(String requestStr) {
        logger.info("优店请求金融中心银盛微信支付"+requestStr);
        Map<String, Object> objMap = JsonUtils.jsonToMap(requestStr);
        Object temp;
        //交易描述
        temp = objMap.get("subject");
        String subject = temp != null ? temp.toString() : null;
        //交易金额
        temp = objMap.get("total_amount");
        String totalAmount = temp != null ? temp.toString() : null;
        //商户自主营销优惠金额
        temp = objMap.get("mer_amount");
        String merAmount = temp != null ? temp.toString() : null;
        //微信用户openId
        temp = objMap.get("sub_openid");
        String subOpenid = temp != null ? temp.toString() : null;
        //平台订单号(三方支付专用的交易单号)
        temp = objMap.get("out_trade_no");
        String outTradeNo = temp != null ? temp.toString() : null;
        //支付类型（ps：银盛  10或汇收银）
        temp = objMap.get("pay_type");
        String payType = temp != null ? temp.toString() : null;
        //支付通道费用
        temp = objMap.get("pay_fee");
        String payFee = temp != null ? temp.toString() : null;
        //对接平台商户号
        temp = objMap.get("store_no");
        String storeNo = temp != null ? temp.toString() : null;
        //平台支付单号
        temp = objMap.get("pay_id");
        String payId = temp != null ? temp.toString() : null;
        //回执地址(通知调用方地址比如够样优店)
        temp = objMap.get("notify_url");
        String notifyUrl = temp != null ? temp.toString() : null;
        //业务类型（例如：支付，退款，提现）
        temp = objMap.get("business_type");
        String businessType = temp != null ? temp.toString() : null;


        //获取支付通道类型id
        if(StringUtils.isEmpty(subject)
                ||StringUtils.isEmpty(totalAmount)||StringUtils.isEmpty(subOpenid)
                ||StringUtils.isEmpty(payFee)||StringUtils.isEmpty(outTradeNo)
                ||StringUtils.isEmpty(storeNo)||StringUtils.isEmpty(payId)
                ||StringUtils.isEmpty(payType)||StringUtils.isEmpty(notifyUrl)
                ||StringUtils.isEmpty(businessType)){
            return ApiResult.paramIsNull();
        }

        //缓存中获取token
        String accessToken = null;
        Object object = redisUtils.getValue("blanceAccessToken");
        if (null == object) {
            accessToken = accessTokenUtil.getaccessToken();
        } else {
            accessToken = (String) object;
        }

        objMap.put("tgAccessToken",accessToken);
        objMap.put("platfromMarking",platfromMarking);
        String result = "";
        try {
            result= httpClientUtils.doPost(UrlConstants.YS_WECHAT_PAY_URL,objMap);
            Map<String, Object> map = JsonUtils.jsonToMap(result);
            logger.info("优店银盛微信支付信息回执内容:{}",JsonUtils.objectToJson(map));
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("YsWechatPayServiceImpl----请求金融中心银盛微信支付接口失败 地址:{},错误信息:{}",UrlConstants.WECHAT_BALANCEPAY_URL,e.getMessage());
        }
        return null;
    }
}
